/**
 * \file       process.cpp
 * \brief      source codes for process multimedia data stream
 * \version    1.0
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-02
 * \remarks    there is initial Version
 * \bug        unknown
 * \todo       process multimedia data
 *
 * <HR>
 * \version    1.1
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-06
 * \remarks    add some function into this class
 *
 * <HR>
 * \version    1.2
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-09
 * \remarks    update function update_progress, add show the current play status informtion
 *
 * <HR>
 * \version    1.3
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-014
 * \remarks    add jump position of media stream.
 */

#include "process.h"

MediaProcess::MediaProcess(void)
{
	 ;
}

MediaProcess::~MediaProcess(void)
{
	 ;
}

void MediaProcess::play(wxMediaCtrl *engineer)
{
     if (engineer != (wxMediaCtrl *)NULL)
     {
		  if (engineer->GetState() != wxMEDIASTATE_PLAYING)
		  {
			   engineer->Play();
		  }
     }
}

void MediaProcess::play(wxMediaCtrl *engineer,wxString& name)
{
     if (engineer != (wxMediaCtrl *)NULL && name !=_T(""))
     {
		  bool load_result = engineer->Load(name);
		  if (!load_result)
		  {
			   wxMessageBox(_T("Can't load"), _T("Error"), wxOK|wxICON_INFORMATION,NULL);
		  }

		  if (engineer->GetState() != wxMEDIASTATE_PLAYING)
		  {
			   engineer->Play();
		  }
     }
}

void MediaProcess::pause(wxMediaCtrl *engineer)
{
     if (engineer != (wxMediaCtrl *)NULL)
     {
		  if (engineer->GetState() != wxMEDIASTATE_PAUSED && 
			  engineer->GetState() != wxMEDIASTATE_STOPPED)
		  {
			   engineer->Pause();
		  }
     }
}

void MediaProcess::stop(wxMediaCtrl *engineer)
{
     if (engineer != (wxMediaCtrl *)NULL)
     {
		  if (engineer->GetState() != wxMEDIASTATE_STOPPED)
		  {
			   engineer->Stop();
		  }
     }
}

void MediaProcess::updata_progress(wxMediaCtrl *engineer,
								   wxGauge* progress,
								   wxStaticText* progress_info)
{
     if (engineer != (wxMediaCtrl *)NULL)
     {
		  if (engineer->GetState() == wxMEDIASTATE_PLAYING || 
			  engineer->GetState() == wxMEDIASTATE_PAUSED)
		  {
			   wxLongLong llLength = engineer->Length();
			   int Minutes = (int) (llLength / 60000).GetValue();
			   int Seconds = (int) ((llLength % 60000)/1000).GetValue();
			   wxLongLong llTell = engineer->Tell();
			   int nMinutes = (int) (llTell / 60000).GetValue();
			   int nSeconds = (int) ((llTell % 60000)/1000).GetValue();
			   progress->SetRange((long)(llLength / 1000).GetValue());
			   progress->SetValue((long)(llTell / 1000).GetValue());

			   wxString cur = wxString::Format(_T("%d:%d"),nMinutes,nSeconds);
			   wxString max = wxString::Format(_T("%d:%d"),Minutes,Seconds);

			   p_info.Clear();
			   if (engineer->GetState() == wxMEDIASTATE_PLAYING)
			   {
					p_info=_T(" playing ");
			   }
			   else
			   {
					p_info=_T(" pause  ");					
			   }
			   p_info.Append(cur);
			   p_info.Append(_T("/"));
			   p_info.Append(max);
			   progress_info->SetLabel(p_info);
		  }
		  else
		  {
			   progress->SetValue(0);
			   progress_info->SetLabel(_T(" Stop Status Now"));
		  }
     }
}

void MediaProcess::set_media_seek(wxMediaCtrl *engineer,uint32_t flag)
{
     if (engineer != (wxMediaCtrl *)NULL)
     {
		  if (engineer->GetState() != wxMEDIASTATE_STOPPED)
		  {			   
			   if (flag==0)
			   {	
					if(engineer->GetState() == wxMEDIASTATE_PLAYING)
					{
						 pause(engineer);
						 if( engineer->Seek(engineer->Length()/jump_time,wxFromCurrent) == wxInvalidOffset)
							  wxMessageBox(wxT("Couldn't seek in movie!"));
						 play(engineer);						 

					}
					else
					{
						 if( engineer->Seek(engineer->Length()/jump_time,wxFromCurrent) == wxInvalidOffset)
							  wxMessageBox(wxT("Couldn't seek in movie!"));
					}
			   }
			   else
			   {
					if(engineer->GetState() == wxMEDIASTATE_PLAYING)
					{
						 pause(engineer);
						 if( engineer->Seek(-(engineer->Length()/jump_time),wxFromCurrent) == wxInvalidOffset)
							  wxMessageBox(wxT("Couldn't seek in movie!"));
						 play(engineer);						 

					}
					else
					{
						 if( engineer->Seek(-(engineer->Length()/jump_time),wxFromCurrent) == wxInvalidOffset)
							  wxMessageBox(wxT("Couldn't seek in movie!"));
					}
			   }					
		  }
	 }	 
}
								  
